package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.ByBsSubjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BySubjectClassifyMapper;
import com.ruoyi.system.domain.BySubjectClassify;
import com.ruoyi.system.service.IBySubjectClassifyService;

@Service
public class BySubjectClassifyServiceImpl implements IBySubjectClassifyService  {
    @Autowired
    private BySubjectClassifyMapper bySubjectClassifyMapper;

    @Autowired
    private ByBsSubjectMapper bsSubjectMapper;


    @Override
    public List<JSONObject> getNextSubjectList(String id) {
        return bySubjectClassifyMapper.getNextSubjectList(id);
    }

    @Override
    public List<JSONObject> getAllSubjectNoTopForById(JSONObject json) {
        String bsid = json.getString("bsid");

        List<JSONObject> stageList = bySubjectClassifyMapper.getStageList();
        for(JSONObject stage:stageList){
            stage.remove("subject");
            List<JSONObject> list = bySubjectClassifyMapper.getAllSubjectNoTopForById(bsid, stage.getString("id"));
            List<JSONObject> listnew = new ArrayList<>();
            for(JSONObject jsonEntity:list){
                List<JSONObject> listChild = new ArrayList<>();
                listChild = bySubjectClassifyMapper.getAllSubjectNoTopForById(bsid, jsonEntity.getString("id"));
                if(listChild != null){
                    jsonEntity.remove("subject");
                    for(JSONObject jsonChild:listChild){
                        if(jsonChild.getInteger("flag") == 1){
                            jsonChild.remove("flag");
                            jsonChild.put("flag",true);
                        }else{
                            jsonChild.remove("flag");
                            jsonChild.put("flag",false);
                        }
                        listnew.add(jsonChild);
                    }
//                    jsonEntity.put("subject",listChild);
                }
                if(jsonEntity.getInteger("flag") == 1){
                    jsonEntity.remove("flag");
                    jsonEntity.put("flag",true);
                }else{
                    jsonEntity.remove("flag");
                    jsonEntity.put("flag",false);
                }
                listnew.add(jsonEntity);
            }
            stage.put("subject",listnew);
        }
        return stageList;
    }

    @Override
    public List<JSONObject> getAllSubjectNoTop() {
        return bySubjectClassifyMapper.getAllSubjectNoTop();
    }

    @Override
    public List<JSONObject> querySubjectForPid(String pid) {
        return bySubjectClassifyMapper.querySubjectClassifyByPid(pid);
    }

    @Override
    public List<BySubjectClassify> queryTopSubject() {
        return bySubjectClassifyMapper.topBySubjectClassify();
    }

    @Override
    public List<BySubjectClassify> queryAllTable() {
        List<BySubjectClassify> newlist = new ArrayList<>();
        //获取顶层的一级栏目
        List<BySubjectClassify> list = bySubjectClassifyMapper.topBySubjectClassify();
        for(BySubjectClassify entity:list){
            List<JSONObject> clist = bySubjectClassifyMapper.querySubjectClassifyByPid(entity.getId() + "");
            entity.setChildren(clist);
            for(JSONObject json:clist){
                List<JSONObject> cclist = bySubjectClassifyMapper.querySubjectClassifyByPid(json.getString("id"));
                json.put("children",cclist);
            }
            newlist.add(entity);
        }
        return newlist;
    }

    @Override
    public List<BySubjectClassify> queryAllTableForBs(String bsid) {
        List<BySubjectClassify> newlist = new ArrayList<>();
        //获取顶层的一级栏目
        List<BySubjectClassify> list = bySubjectClassifyMapper.topBySubjectClassify();
        for(BySubjectClassify entity:list){
            List<JSONObject> clist = bySubjectClassifyMapper.querySubjectClassifyByPid(entity.getId() + "");
            List<JSONObject> resultlist = new ArrayList<>();
            for(JSONObject json:clist){
                String id = json.getString("id");
                List<JSONObject> objlist = bsSubjectMapper.getEntityBySubjectIdAndBsId(id, bsid);
                if(objlist.size() > 0){
                    json.put("status",true);
                }else {
                    json.put("status",false);
                }
                resultlist.add(json);
            }
            entity.setChildren(resultlist);
            for(JSONObject json:clist){
                List<JSONObject> cclist = bySubjectClassifyMapper.querySubjectClassifyByPid(json.getString("id"));
                resultlist = new ArrayList<>();
                for(JSONObject jsoncc:cclist){
                    String id = jsoncc.getString("id");
                    List<JSONObject> objlist = bsSubjectMapper.getEntityBySubjectIdAndBsId(id, bsid);
                    if(objlist.size() > 0){
                        jsoncc.put("status",true);
                    }else {
                        jsoncc.put("status",false);
                    }
                    resultlist.add(jsoncc);
                }
                json.put("children",resultlist);
            }
            newlist.add(entity);
        }
        return newlist;
    }

    /**
     * 查询学科分类
     *
     * @param id 学科分类主键
     * @return 学科分类
     */
    @Override
    public BySubjectClassify selectBySubjectClassifyById(Long id)
    {
        return bySubjectClassifyMapper.selectBySubjectClassifyById(id);
    }

    /**
     * 查询学科分类列表
     *
     * @param bySubjectClassify 学科分类
     * @return 学科分类
     */
    @Override
    public List<BySubjectClassify> selectBySubjectClassifyList(BySubjectClassify bySubjectClassify)
    {
        return bySubjectClassifyMapper.selectBySubjectClassifyList(bySubjectClassify);
    }

    /**
     * 新增学科分类
     *
     * @param bySubjectClassify 学科分类
     * @return 结果
     */
    @Override
    public int insertBySubjectClassify(BySubjectClassify bySubjectClassify)
    {
        bySubjectClassify.setAddtime(DateUtils.getTime());
        return bySubjectClassifyMapper.insertBySubjectClassify(bySubjectClassify);
    }

    /**
     * 修改学科分类
     *
     * @param bySubjectClassify 学科分类
     * @return 结果
     */
    @Override
    public int updateBySubjectClassify(BySubjectClassify bySubjectClassify)
    {
        return bySubjectClassifyMapper.updateBySubjectClassify(bySubjectClassify);
    }

    /**
     * 批量删除学科分类
     *
     * @param ids 需要删除的学科分类主键
     * @return 结果
     */
    @Override
    public int deleteBySubjectClassifyByIds(Long[] ids)
    {
        return bySubjectClassifyMapper.deleteBySubjectClassifyByIds(ids);
    }

    /**
     * 删除学科分类信息
     *
     * @param id 学科分类主键
     * @return 结果
     */
    @Override
    public int deleteBySubjectClassifyById(Long id)
    {
        return bySubjectClassifyMapper.deleteBySubjectClassifyById(id);
    }
}
